package cn.zptc.springbootdemo.service;

import cn.zptc.springbootdemo.domain.Comment;
import cn.zptc.springbootdemo.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author ：zhaoweiwei
 * @date ：创建于 2025/3/27 13:41
 * @description：
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Cacheable(cacheNames = "comment")
    public Comment findById(int comment_id){
        Optional<Comment> optional = commentRepository.findById(comment_id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    public Comment updateComment(Comment comment){
        commentRepository.updateComment(comment.getAuthor(), comment.getId());
        return comment;
    }

    public void deleteComment(int comment_id){
        commentRepository.deleteById(comment_id);
    }

    // 新增方法：查询作者不为空的评论
    @Cacheable(cacheNames = "commentsByAuthorNotNull")
    public List<Comment> findCommentsByAuthorNotNull() {
        return commentRepository.findByAuthorNotNull();
    }
}